<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
include_once ("secure_area.php");

class Advertisements extends Secure_area {

	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URLa
	 * 		http://example.com/index.php/welcome
	 *	- or -
	 * 		http://example.com/index.php/welcome/index
	 *	- or -
	 * Since this controller is set as the default controller in
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see http://codeigniter.com/user_guide/general/urls.html
	 */
	public function __construct(){
		parent::__construct();
		$this->load->helper('Boolean_helper');
		$this->load->model('admin_advertisements_model');
	}

	public function index(){
		
		$this->page();
	}

	function page($start_row=0){

		$limit = 2;
		if(trim($start_row) == ""){
			$start_row = 0;
		}
		$data['title']= "Advertisements";
		$data['main_content'] = 'admin/ads_listing';
		//listing
		$testi_qry = $this->admin_advertisements_model->getAdvertisements();

		
		$data['posts_qry'] = $testi_qry;
		
		//pagination
		$this->load->library('pagination');
		$config['base_url'] = site_url('admin/advertisements/page/');
		$config['total_rows'] = $testi_qry->num_rows();
		$config['per_page'] = $limit;
		$config['uri_segment'] = 4;
		$config['anchor_class'] = 'class="button pagi" ';
		$this->pagination->initialize($config);
		$data['pagination'] = $this->pagination->create_links();
		$data['testi_qry_limited'] = $this->admin_advertisements_model->getAdvertisements_limited( $start_row, $limit );
		$this->load->view('admin/includes/template', $data);
	}

	// upload banner
	//return path or error
	function upload(){
		//print_r($_FILES);exit();
		$newfilename = md5($_SERVER['REMOTE_ADDR'].rand());
		$config = array(
			'allowed_types' => 'gif|jpg|png|bmp|jpeg',
			'upload_path' => $_SERVER['DOCUMENT_ROOT'].'/mlme/assets/images/banner/',
			'max_size' => 3000,
			'max_width' => 500,
			'max_height' => 500,
			'file_name' => $newfilename
		);
		$this->load->library('upload', $config);

		$this->upload->initialize($config);

		if ( ! $this->upload->do_upload('banner')){
			$error = array('error' => $this->upload->display_errors());
			return $error;
		} else {
			$upload = array('upload_data' => $this->upload->data());

			$path = '/banner/'.$upload['upload_data']['file_name'];
			return $path;

		}
	}

	function save(){
		$banner = $this->upload();
		
		$errmsg = '';
		if(is_array($banner)){
			echo '';
			$banner_path = '';
			$errmsg = $banner['error'];
		} else {
			$banner_path = $banner;
		}

		$_POST['banner'] = $banner_path;
	
		$this->admin_advertisements_model->addAdvertisements($_POST);
		redirect('admin/advertisements/');
	}

	function edit($id){
		$data['title']= "Edit";
		$data['main_content'] = 'admin/ads_form';
		$ads = $this->admin_advertisements_model->getAdvertisements('WHERE advertisementsid='.$id);
		$data['row'] = $ads->row();

		$this->load->view('admin/includes/template', $data);
	}

	function update($id){
		$post = $this->input->post();
		$query = $this->admin_advertisements_model->updateAdvertisements($post,$id);
		return $query;
	}



	function create(){
		$data['title'] = 'Advertisements';
		$data['main_content'] = 'admin/ads_form';
		$this->load->view('admin/includes/template', $data);
	}

}
